var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var fs = require('fs');
var FileStreamRotator = require('file-stream-rotator');

var index = require('./routes/index');
var users = require('./routes/users');
var login = require('./routes/login');
var bj = require('./routes/bj');
var students = require('./routes/students');
var booksType = require('./routes/booksType');
var booksInfor = require('./routes/booksInfor');
var borrow = require('./routes/borrow');



// const log4js= require('./log')
// const logger = log4js.getLogger()
// const errlogger = log4js.getLogger('err')
// const othlogger = log4js.getLogger('oth')

var app = express();


// log4js.useLogger(app,logger)
//这样会自动记录每次请求信息，放在其他use上面
//手动记录，可以代替console.log
// logger.info('test info 1')
// errlogger.error('test error 1')
// othlogger.info('test info 2')

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));

//任何域名都能访问服务器
//Access-Control-Request-Headers 于通知服务器在真正的请求中会采用哪些请求首部
app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Authorization,Origin, X-Requested-With, Content-Type, Accept, Host, X-Token");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",'3.2.1');
 
  //res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use('/', index);
app.use('/api/users', users);
app.use('/api/login', login);
app.use('/api/bj', bj);
app.use('/api/students', students);
app.use('/api/booksType', booksType);
app.use('/api/booksInfor', booksInfor);
app.use('/api/borrow', borrow);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
